package xuhaijia.lanou3g.autohome.myself;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;
import xuhaijia.lanou3g.autohome.db.DBToolForHistory;
import xuhaijia.lanou3g.autohome.recommend.originalvideo.VideoPlayerActivity;

/**
 * Created by 7 on 16/8/31.
 */
public class HistoryActivity extends BaseActivity implements View.OnClickListener {

    private ListView historyLv;
    private HistoryAdapter historyAdapter;
    private TextView clear;

    @Override
    protected int setLayout() {
        return R.layout.activity_history;
    }

    @Override
    protected void initView() {
        historyLv = bindView(R.id.activity_history_lv);
        clear = bindView(R.id.activity_history_clear);
        clear.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        historyAdapter = new HistoryAdapter(this);
        historyLv.setAdapter(historyAdapter);
        DBToolForHistory.getInstance().queryAllHistory(new DBToolForHistory.QueryListener() {
            @Override
            public void onQueryComplete(final ArrayList<History> histories) {
                Collections.sort(histories ,new HistoryOrder());
                historyAdapter.setHistory(histories);

                historyLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String url =  histories.get(position).getUrl();
                        Intent intent = new Intent(HistoryActivity.this ,VideoPlayerActivity.class);
                        intent.putExtra("url" , url);
                        startActivity(intent);
                    }
                });
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_history_clear:
                DBToolForHistory.getInstance().clearHistory();
                DBToolForHistory.getInstance().queryAllHistory(new DBToolForHistory.QueryListener() {
                    @Override
                    public void onQueryComplete(ArrayList<History> histories) {
                        historyAdapter.setHistory(histories);
                    }
                });
                break;
        }

    }

    private class HistoryOrder implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            History p1 = (History) lhs;
            History p2 = (History) rhs;
            return new Long(p2.getLookTime()).compareTo(new Long(p1.getLookTime()));
        }
    }
}
